package com.gimi.cloud.jimi.manage.consumer.service.app.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.Validates;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.AdDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.AdAndColumnFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.app.AdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @auther loongs
 * @date 2017/12/27  14:41
 */
@Service
public class AdServiceImpl implements AdService {
    private static final Logger logger = LoggerFactory.getLogger(AdServiceImpl.class);
    private AdAndColumnFeignService adAndColumnFeignService;
    @Autowired
    public AdServiceImpl (AdAndColumnFeignService adAndColumnFeignService) {
       this.adAndColumnFeignService=adAndColumnFeignService;
    }
    @Override
    public Response getAdList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        Map<String,String> map=JsonUtil.jsonToMap(requestBody);
        String pageId = map.get("pageId");
        if(!StringUtil.isBlank(pageId) && pageId.length() > 9){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"页面编号填写数字，且不能超过9位");
        }

        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getPageSize())||StringUtil.isBlank(adDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.getAdList(adDTO);
    }

    @Override
    public Response adRelyColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getColumnId()) || StringUtil.isBlank(adDTO.getCurrentPage()) || StringUtil.isBlank(adDTO.getPageSize())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.adRelyColumnProd(adDTO);
    }

    @Override
    public Response addAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        //判断数字长度
        Validates.checkNumLength(requestBody);
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getColumnId())
                ||StringUtil.isBlank(adDTO.getComent())||StringUtil.isBlank(adDTO.getPageId())||StringUtil.isBlank(adDTO.getImgUrl())
                ||StringUtil.isBlank(adDTO.getPositionNum())||StringUtil.isBlank(adDTO.getAdClass())
                ||StringUtil.isBlank(adDTO.getTitle())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        //判断广告类型
        checkAdClassAndAdObject(adDTO);
        return adAndColumnFeignService.addAd(adDTO);
    }

    @Override
    public Response editAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        //判断数字长度
        Validates.checkNumLength(requestBody);
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getColumnId())
                ||StringUtil.isBlank(adDTO.getComent())||StringUtil.isBlank(adDTO.getPageId())||StringUtil.isBlank(adDTO.getImgUrl())
                ||StringUtil.isBlank(adDTO.getPositionNum())||StringUtil.isBlank(adDTO.getAdClass())
                ||StringUtil.isBlank(adDTO.getTitle())||StringUtil.isBlank(adDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        //判断广告类型
        checkAdClassAndAdObject(adDTO);
        return adAndColumnFeignService.editAd(adDTO);
    }


    /**
     * 判断广告类型
     * @param adDTO
     */
    private void checkAdClassAndAdObject(AdDTO adDTO){
        //如果广告类型为3，说明广告类别是静态页面的转跳路径
        if(3 == adDTO.getAdClass()){
            if(StringUtil.isBlank(adDTO.getTargetUrl())){
                throw new MsgException(ResponseCodeEnum.TARGET_URL_IS_EMPTY);
            }
        }
        //如果广告类型为1，商品名称不能为空
        if(1 == adDTO.getAdClass()){
            if(StringUtil.isBlank(adDTO.getAdObject())){
                throw new MsgException(ResponseCodeEnum.AD_PRODUCT_IS_NOT_EMPTY);
            }
            if( StringUtil.isBlank(adDTO.getPrice())||StringUtil.isBlank(adDTO.getOriginalPrice())){
                throw new MsgException(ResponseCodeEnum.PRICE_IS_NOT_EMPTY);
            }
        }
    }

    @Override
    public Response gotoEditAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.gotoEditAd(adDTO);
    }

    @Override
    public Response deleteAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.deleteAd(adDTO);
    }

    @Override
    public Response gotoAddAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        return adAndColumnFeignService.gotoAddAd(adDTO);
    }

    @Override
    public Response publishAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getId())||StringUtil.isBlank(adDTO.getPublishTime())||StringUtil.isBlank(adDTO.getEndTime())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.publishAd(adDTO);
    }

    @Override
    public Response underAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AdDTO adDTO = JsonUtil.jsonToBean(requestBody,AdDTO.class);
        if(StringUtil.isBlank(adDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.underAd(adDTO);
    }

    @Override
    public Response uploadAdFile(HttpServletResponse response, HttpServletRequest request) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
//        List<String> list=new LinkedList<>();
//        list.add(request.getParameter("type"));
//        multipartFiles.put("type",list);
        return adAndColumnFeignService.uploadAdFile(multipartFiles);
    }

}
